package com.hyx.api.service.impl.pc_admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hyx.api.domain.User;
import com.hyx.api.domain.bo.UserBo;
import com.hyx.api.domain.vo.AppUserVo;
import com.hyx.api.enums.UserTypeEnum;
import com.hyx.api.mapper.UserMapper;
import com.hyx.api.service.pc_admin.IUserService;
import com.hyx.api.utils.MD5Util;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 用户列表Service业务层处理
 *
 * @author HK
 * @date 2023-10-23
 */
@RequiredArgsConstructor
@Service
public class UserServiceImpl implements IUserService {
    private final UserMapper userMapper;

    /**
     * 查询用户列表
     *
     * @param id 用户列表主键
     * @return 用户列表
     */
    @Override
    public AppUserVo selectUserById(Long id) {
        //TODO 表以外的字段，写入vo
        AppUserVo appUserVo = userMapper.selectVoById(id);
        return appUserVo;
    }

    /**
     * 查询用户列表列表
     *
     * @param bo 用户列表
     * @return 用户列表
     */
    @Override
    public List<AppUserVo> selectUserList(UserBo bo) {
        LambdaQueryWrapper<User> lqw = Wrappers.lambdaQuery();
        lqw.like(ObjectUtils.isNotEmpty(bo.getUsername()), User::getUsername, bo.getUsername());
        lqw.eq(ObjectUtils.isNotEmpty(bo.getPhone()), User::getPhone, bo.getPhone());
        lqw.like(ObjectUtils.isNotEmpty(bo.getNickName()), User::getNickName, bo.getNickName());
        lqw.eq(ObjectUtils.isNotEmpty(bo.getType()), User::getType, bo.getType());
        lqw.eq(ObjectUtils.isNotEmpty(bo.getRecoveryId()), User::getRecoveryId, bo.getRecoveryId());
        lqw.eq(ObjectUtils.isNotEmpty(bo.getCompanyId()), User::getCompanyId, bo.getCompanyId());
        lqw.eq(ObjectUtils.isNotEmpty(bo.getStoreId()), User::getStoreId, bo.getStoreId());
        lqw.eq(ObjectUtils.isNotEmpty(bo.getSysUserId()), User::getSysUserId, bo.getSysUserId());
        lqw.eq(User::getDelFlag, 0);
        return userMapper.selectVoList(lqw);
    }

    /**
     * 新增用户列表
     *
     * @param bo 用户列表
     * @return 结果
     */
    @Override
    public int insertUser(UserBo bo) {
        User user = tranEntityBeforeSave(bo);
        user.setCreateBy(SecurityUtils.getUsername());
        user.setCreateTime(DateUtils.getNowDate());
        user.setCreateId(SecurityUtils.getUserId());
        user.setDelFlag(0);
        validEntityBeforeSave(user);
        return userMapper.insert(user);
    }

    /**
     * 修改用户列表
     *
     * @param bo 用户列表
     * @return 结果
     */
    @Override
    public int updateUser(UserBo bo) {
        User user = tranEntityBeforeSave(bo);
        user.setUsername(null);
        user.setType(null);
        user.setRecoveryId(null);
        user.setCompanyId(null);
        return userMapper.updateById(user);
    }

    /**
     * 批量删除用户列表
     *
     * @param ids 需要删除的用户列表主键
     * @return 结果
     */
    @Override
    public int deleteUserByIds(Long[] ids) {
        return userMapper.deleteUserByIds(Arrays.asList(ids));
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(User entity) {
        //TODO 做一些数据校验,如唯一约束
        LambdaQueryWrapper<User> lqw = Wrappers.lambdaQuery();
        lqw.eq(User::getType, entity.getType());
        Long limitCount = null;
        if (UserTypeEnum.SUM_ACCOUNT.code().equals(entity.getType())) {
            lqw.eq(User::getRecoveryId, entity.getRecoveryId());
            limitCount = userMapper.selectCount(lqw);
        } else if (UserTypeEnum.COMPANY.code().equals(entity.getType())) {
            lqw.eq(User::getCompanyId, entity.getCompanyId());
            limitCount = userMapper.selectCount(lqw);
        } else if (UserTypeEnum.SHOP_OWNER.code().equals(entity.getType())) {
            lqw.eq(User::getStoreId, entity.getStoreId());
            limitCount = userMapper.selectCount(lqw);
        }
        if (limitCount != null && limitCount > 0) {
            throw new ServiceException("不能重复创建");
        }
        lqw.clear();
        lqw.eq(User::getUsername, entity.getUsername());
        if (userMapper.selectCount(lqw) > 0) {
            throw new ServiceException("该账号已存在");
        }
    }

    /**
     * 保存前的数据转换
     */
    private User tranEntityBeforeSave(UserBo bo) {
        //TODO 做一些数据转换
        User user = new User();
        BeanUtils.copyProperties(bo, user);
        if (ObjectUtils.isNotEmpty(user.getPassword())) {
            user.setPassword(MD5Util.encrypt32(user.getPassword()));
        }
        return user;
    }
}
